package com.lyx.shopcard.controller;


import com.lyx.shopcard.entity.Good;
import com.lyx.shopcard.service.IGoodService;
import com.lyx.shopcard.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liyanxi
 * @since 2021-07-25
 */
@RestController
@RequestMapping("/good")
public class GoodController {
    @Autowired
    private IGoodService iGoodService;

    @GetMapping("/getGoodList")
    public Result getGood(){
        List<Good> goods = iGoodService.list();
        return Result.ok(goods);
    }

    @PostMapping("/addGood")
    public Result addGood(@RequestBody Good good){
        good.setCreatTime(LocalDateTime.now());
        good.setUpdateTime(LocalDateTime.now());
        boolean save = iGoodService.save(good);
        if(save){
          return Result.ok(good,"新增商品成功");
        }else {
            return Result.error("新增失败");
        }
    }

    @PostMapping("/updateGood")
    public Result updateGood(@RequestBody Good good){
        good.setUpdateTime(LocalDateTime.now());
        boolean update = iGoodService.updateById(good);
        if(update){
            return Result.ok(good,"更新商品成功");
        }else {
            return Result.error("更新商品失败");
        }
    }

    @PostMapping("/deleteGood/{id}")
    public Result deleteGood(@PathVariable("id") int id){
        boolean b = iGoodService.removeById(id);
        if(b){
            return Result.ok("删除商品成功");
        }else {
            return Result.error("删除商品失败");
        }
    }

}
